
char uart_rd;
unsigned char pt[10];
char *temp = "0000";
int adc;
int mod_val;

// LCD module connections

sbit buz at RC0_bit;
sbit pump at RD1_bit;
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections
void percent(int a);
void main()
{  
   TRISD.F1=0;
   buz=0;
   pump=0;
   ADCON1 = 0x80;
   TRISA = 0xff;
   Lcd_Init(); // Initialize LCD
   Lcd_Cmd(_LCD_CLEAR); // Clear display
   Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
   Lcd_Out(1,1,"Soil Moisture");//Write text'Hello World' in first row
   Lcd_Out(2,1,"Auto Irrigation");
  UART1_Init(9600);
  Delay_ms(2000);
  while(1)
  {
   Lcd_Cmd(_LCD_CLEAR);
   adc = ADC_Read(1);
     temp[0] = adc/1000 +48 ; // Add 48 to get the ASCII character value
     temp[1] = (adc/100)%10 +48;
     temp[2] = (adc/10)%10 +48;
     temp[3] = adc%10 +48;

   


   Lcd_Out(1,1,"Moisture=");
   percent(adc);
    //Lcd_Out(2,10,temp);
//   Lcd_Out(1,10,pt);
   Lcd_Chr_Cp(37);

   if (adc>451)
     { // Delay_ms(1000);
       Lcd_Out(2,1,"Motor Off");// and send data via UART
       pump=0;
       Delay_ms(1000);
      }
   else
    { 
       if(pump==0)
       {buz=1;
        Delay_ms(1000);
        buz=0;
       }
        Lcd_Out(2,1,"Motor On ");// and send data via UART
        pump=1;
        Delay_ms(1000);
       }
   }
  }
  
  
  void percent(int a)
  {
    if(a<100)
    {
      Lcd_Out(1,10," 00");
    }
    else if(101<a && a<450)
    {
      Lcd_Out(1,10," 20");
    }
    else if(451<a && a<580)
    {
      Lcd_Out(1,10," 40");
    }
    else if(581<a && a<610)
    {
      Lcd_Out(1,10," 60");
    }
    else if(611<a && a<645)
    {
      Lcd_Out(1,10," 80");
    }
    else if(646<a)
    {
      Lcd_Out(1,10,"100");
    }
    
  }